package com.cg.asm.core.config.security;

import com.alibaba.fastjson.JSONObject;
import com.cg.asm.core.dao.user.UserMapper;
import com.cg.asm.core.domain.vo.user.UserLoginVO;
import com.cg.asm.core.service.menu.IMenuService;
import com.cg.asm.core.util.AsmApiUtils;
import com.cg.asm.core.util.AsmCommonUtil;
import com.cg.asm.client.CommonUtil;
import com.cg.asm.client.constans.ConstantsEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Component
public class ResultAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    private IMenuService menuService;
    private UserMapper userMapper;
    @Value("${asm.config.hostUrl}")
    private String hostUrl;

    @Autowired
    public ResultAuthenticationSuccessHandler(IMenuService menuService, UserMapper userMapper) {
        this.menuService = menuService;
        this.userMapper = userMapper;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        PrintWriter out = httpServletResponse.getWriter();
        JSONObject resultInfo;
        UserLoginVO userLoginVO = AsmApiUtils.getCurrentUserLoginVO();
        if (null != userLoginVO) {
            // 次日登陆初始锁定信息
            if (null != userLoginVO.getErrorLoginNumber()) {
                userMapper.updateLockInit(userLoginVO.getUserId());
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("userInfo", AsmCommonUtil.UserLoginVOToUserInfo(userLoginVO));
            resultMap.put("tokenValue", AsmApiUtils.tokenHandle(httpServletRequest.getSession().getId()));
            resultMap.put("hostUrl", hostUrl);
            /**
             * platformTypeCode :平台类型编码，用于获取指定平台的功能清单如：PC端
             * 备注：功能清单第一级则为平台类型
             */
            String platformTypeCode = httpServletRequest.getParameter("platformTypeCode");
            resultMap.put("menuList", menuService.getUserMenuInfo(userLoginVO.getUserId(), userLoginVO.getUserGroupId(), platformTypeCode));
            resultInfo = CommonUtil.successJson(resultMap);
            httpServletRequest.getSession().setAttribute(AsmCommonUtil.ASMCONTEXELOGINKEY, resultInfo);
        } else {
            String returnCode = ConstantsEnum.RESPONSE_E_AAAD.getKey();
            String returnMsg = ConstantsEnum.RESPONSE_E_AAAD.getValue();
            resultInfo = CommonUtil.errorJson(returnCode, returnMsg);
        }
        AsmApiUtils.outPrintlnByJSONObject(out, resultInfo);
    }
}
